//
//  DComputeItem.m
//  dance
//
//  Created by aether on 2023/10/20.
//

#import "DComputeItem.h"

#define ITEM_SPACE  10

@interface DComputeItem ()

@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *title;
@property (nonatomic, strong) UILabel *remind;
@property (nonatomic, strong) NSDictionary *dict;

@end

@implementation DComputeItem

- (id) initWithFrame:(CGRect)frame withDic:(NSDictionary *) dict {
    self = [super initWithFrame:frame];
    if (self) {
        self.dict = dict;
        [self initViews];
    }
    
    return self;;
}

- (void) initViews {
    [self addSubview:self.icon];
    [self addSubview:self.title];
    [self addSubview:self.remind];
    [self setBackgroundColor:[self randomColor]];
}

#pragma mark - 辅助方法

- (UIColor *)randomColor {
    CGFloat red = (CGFloat)arc4random_uniform(256) / 255.0;
    CGFloat green = (CGFloat)arc4random_uniform(256) / 255.0;
    CGFloat blue = (CGFloat)arc4random_uniform(256) / 255.0;
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}

#pragma mark - 设置获取

- (UIImageView *) icon {
    if (_icon == NULL) {
        UIImage *image = [UIImage imageNamed:[self.dict objectForKey:@"name"]];
        CGFloat height = 85;
        CGFloat width = (height * image.size.width) / image.size.height;
        _icon = [[UIImageView alloc] initWithImage:image];
        [_icon setFrame:CGRectMake((self.frame.size.width - width) / 2, ITEM_SPACE, width, height)];
    }
    
    return _icon;
}

- (UILabel *) title {
    if (_title == NULL) {
        _title = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 55, self.frame.size.width, 20)];
        [_title setFont:[UIFont boldSystemFontOfSize:20]];
        [_title setText:[self.dict objectForKey:@"name"]];
        [_title setTextAlignment:NSTextAlignmentCenter];
    }
    
    return _title;
}

- (UILabel *) remind {
    if (_remind == NULL) {
        _remind = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 25, self.frame.size.width, 17)];
        [_remind setTextColor:[UIColor lightGrayColor]];
        [_remind setFont:[UIFont systemFontOfSize:16]];
        [_remind setText:[self.dict objectForKey:@"remind"]];
        [_remind setTextAlignment:NSTextAlignmentCenter];
    }
    
    return _remind;
}

@end
